package com.x.crm.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.x.crm.DocDesc;
import com.x.crm.model.*;
import com.x.crm.rpc.*;
import com.x.erp.model.*;
import com.x.erp.rpc.*;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.security.rpc.SecurityRpcService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class FollowController {

	private Logger log = LogManager.getLogger(this.getClass());
	@Reference
	private IntentionRpcService intentionRpcService;
	@Reference
	private UserRpcService userRpcService;
	@Reference
	private CrmSysParamRpcService crmSysParamRpcService;

	@Reference
	private SecurityRpcService securityRpcService;

	@Reference
	private ProvinceRpcService provinceRpcService;
	@Reference
	private CityRpcService cityRpcService;
	@Reference
	private BrandRpcService brandRpcService;
	@Reference
	private SeriesRpcService seriesRpcService;
	@Reference
	private CarRpcService carRpcService;
	@Reference
	private FollowRpcService followRpcService;
	@Reference
	private IntentionModelRpcService intentionModelRpcService;
	@Reference
	private CustomerRpcService customerRpcService;


	@DocDesc("省列表")
	@RequestMapping("/provinces")
	public List<Province> listProvince() {
		return provinceRpcService.selectProvinceList(new Province());
	}

	@DocDesc("市列表")
	@RequestMapping("/cities")
	public List<City> listCity(City city) {
		return cityRpcService.selectCityList(city);
	}

	@DocDesc("品牌列表")
	@RequestMapping("/brands")
	public List<Brand> listBrand() {
		return brandRpcService.selectBrandList(new Brand());
	}

	@DocDesc("车系列表")
	@RequestMapping("/series")
	public List<Series> listSeries(Series series) {
		return seriesRpcService.selectSeriesList(series);
	}


	@RequestMapping(value = "/crmIntentionCars", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel crmIntentionCars(Car car) {
		car.setCarStockState(0);
		List<Car> carListStockState0 = carRpcService.selectCarList(car);
		car.setCarStockState(10);
		List<Car> carListStockState10 = carRpcService.selectCarList(car);
		car.setCarStockState(20);
		List<Car> carListStockState20 = carRpcService.selectCarList(car);
		carListStockState0.addAll(carListStockState10);
		carListStockState0.addAll(carListStockState20);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carListStockState0);
	}


	@RequestMapping(value = "/crmIntentionCarNums", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel crmIntentionCarNums(Car car) {
		car.setCarStockState(0);
		int carListStockState0 = carRpcService.selectCarNum(car);
		car.setCarStockState(10);
		int carListStockState10 = carRpcService.selectCarNum(car);
		car.setCarStockState(20);
		int carListStockState20 = carRpcService.selectCarNum(car);
		int carNums = carListStockState0 + carListStockState10 + carListStockState20;
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carNums);
	}



	@DocDesc("查询手机号是否重复")
	@RequestMapping(value = "/checkPhone", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel checkPhone(Customer customer) {
		List<Customer> customerList = customerRpcService.selectCustomerList(customer);
		Map<String, Object> res = new HashMap<>();
		res.put("isExist", customerList.size() != 0);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}


}
